<?php
	require_once 'bud_mng'.EXT;

	class Bud_activity extends Bud_mng {
		protected $model_path = 'bud/m_activity';
		protected $detail_model_path = 'bud/m_program';
		
		function index(){
			redirect($this->common->get_application_description($prefix).'/'.get_unit_controller($this).'/view');
		}
		
		function _make_option(){
			$option[] = array();

			//----------------------------------- build Option

			$data_kpu= $this->load->model('m_kpu')->select_query('key','','key');
			

			$option['option_kpu'] = array();
			foreach($data_kpu as $row){
				$option['option_kpu'][$row->key]=$row->key;
			}
			
			//------
			$data_pu= $this->load->model('m_pu')->select_query('key','','key');
			

			$option['option_pu'] = array();
			foreach($data_pu as $row){
				$option['option_pu'][$row->key]=$row->key;
			}


			//--------
			$data_program = $this->load->model('m_program')->select_query('key','','key');
			
			$option['option_program'] = array();
			foreach($data_program as $row){
				$option['option_program'][$row->key]=$row->key;
			}


			//-----
			$data_source= $this->load->model('m_sumber')->select_query('key','','key');
			

			$option['option_source'] = array();
			foreach($data_source as $row){
				$option['option_source'][$row->key]=$row->key;
			}

			//------
			$data_unit= $this->load->model('m_unit')->select_query('key','','key');
			

			$option['option_unit'] = array();
			foreach($data_unit as $row){
				$option['option_unit'][$row->key]=$row->key;
			}


			//----
			$data_yayasan= $this->load->model('m_yayasan_activity')->select_query('key','','key');
			

			$option['option_yayasan'] = array();
			foreach($data_yayasan as $row){
				$option['option_yayasan'][$row->key]=$row->key;
			}


			//-------------------------------------------------------------------------------
			$data_versi= $this->load->model('m_versi')->select_query('key','','key');
			

			$option['option_versi'] = array();
			foreach($data_versi as $row){
				$option['option_versi'][$row->key]=$row->key;
			}

			return $option;
		}

		function view()
		{
			$option = $this->_make_option();

			if(!isset($_POST['versi']) || $_POST['versi']==NULL){
				$condition['key_bud_m_version'] = $this->versi;
			}else{
				$condition['key_bud_m_version'] = $_POST['versi'];
			}
			if(isset($_POST['kpu']) && trim($_POST['kpu'])!=NULL){
				$condition['SUBSTRING(`key`,1,1)']=$_POST['kpu'];
			}
			if(isset($_POST['pu']) && trim($_POST['pu'])!=NULL){
				$condition['SUBSTRING(`key`,1,4)']=$_POST['pu'];
			}
			if(isset($_POST['program']) && trim($_POST['program'])!=NULL){
				$condition['SUBSTRING(`key`,1,7)']=$_POST['program'];
			}
		
			$content = $this->load->model($this->model_path)->select_query('',$condition);
			
			$versi = $condition['key_bud_m_version']?$condition['key_bud_m_version']:$this->versi;

			$data = array(
				'application' => $prefix,
				'application_title' => $this->common->get_application_description($prefix),
				'page' => 'View_activity',
				'navigation' => $this->load->view('bud/navigation/administrator/navigation', '', true).$this->load->view('bud/navigation/administrator/nav_minor', array('url'=>'activity'), true),
				'content' => $this->load->view('bud/administrator/activity',array(
																	'base' => $this->config->item('base_url').'system/application/modules/',
																				  'menus' => 'view',
																			      'option' => $option,
																				  'versi' => $versi,
																				  'content' => $content),true),
			); 

			$this->load->view('bud/layout/default', $data);
		}
		function add()
		{
			$option = $this->_make_option();


			$data = array(
				'application' => $prefix,
				'application_title' => $this->common->get_application_description($prefix),
				'page' => 'Add_activity',
				'navigation' => $this->load->view('bud/navigation/administrator/navigation', '', true).$this->load->view('bud/navigation/administrator/nav_minor',array('url'=>'activity'), true),
				'content' => $this->load->view('bud/administrator/activity',array(
																		'base' => $this->config->item('base_url').'system/application/modules/',		
																				  'menus' => 'add',
																				  'option' => $option,
																			      'model' => $this->load->model($this->model_path),
																				  'det_model' => $this->load->model($this->detail_model_path)),true),
			); 
			$this->load->view('bud/layout/default', $data);
		}

		function update()
		{
			$option = $this->_make_option();

			$id = $this->uri->segment(4);
			$model = $this->load->model($this->model_path);
			
			$record = $model->select_query('',array('id'=>$id));
			$record = $record[0];

			$data = array(
				'application' => $prefix,
				'application_title' => $this->common->get_application_description($prefix),
				'page' => 'Update_activity',
				'navigation' => $this->load->view('bud/navigation/administrator/navigation', '', true).$this->load->view('bud/navigation/administrator/nav_minor',array('url'=>'activity'), true),
				'content' => $this->load->view('bud/administrator/activity',array(
																		'base' => $this->config->item('base_url').'system/application/modules/',		
																				  'menus' => 'update',
																			      'record' => $record,
																				  'option' => $option,
																				  'det_model' => $this->load->model($this->detail_model_path)),true),
			); 
			$this->load->view('bud/layout/default', $data);
		}

		function insert()
		{
			$pertahun= 0;
			for($i=1;$i<=12;$i++){
				$pertahun += $_POST[$i];
			}

			$total =  $_POST['gaji_honor']+$_POST['barang_tak_habis']+$_POST['barang_habis']+$_POST['perjalanan']+$_POST['jasa_honor_ext'];
			
			$perbulan = $pertahun!=0?$total/$pertahun:0;
			
			$temp_bulan = array();
			
			//array for month
			for($i=1;$i<=12;$i++){
				$temp_bulan[$this->__int_to_month($i)]= $_POST[$i]!=false?$perbulan:0;
			}

			//array_merge(arrays)
			$data = array (
				'key'=>$_POST['id_program'].'.'.$_POST['id_activity'],
				'name_activity'=>$_POST['name_activity'],
				'desc_activity'=>$_POST['name_activity'],
				'total'=>$total,
				'gaji_honor'=>$_POST['gaji_honor'],
				'barang_tak_habis'=>$_POST['barang_tak_habis'],
				'barang_habis'=>$_POST['barang_habis'],
				'perjalanan'=>$_POST['perjalanan'],
				'jasa_honor_ext'=>$_POST['jasa_honor_ext'],
				'volume'=>$_POST['volume'],
				'operational'=>$_POST['operational'],
				'key_bud_m_yayasan_activity'=>$_POST['id_yayasan'],
				'key_bud_r_sumber'=>$_POST['id_source'],
				'key_bud_r_unit'=>$_POST['id_unit'],
				'key_bud_m_version'=>$this->versi,
				'key_bud_m_program'=>$_POST['id_program'],
			);
			

			$data = array_merge($data,$temp_bulan);//merging
			$this->common->who_am_i($data, true, 'crea');
			$this->db->insert('bud_m_d_activity',$data);
			$this->common->flash_message('Succesfully added',-1);	
			redirect("bud/bud_activity/add");
		}
		
		function process_update()
		{
			$pertahun= 0;
			for($i=1;$i<=12;$i++){
				$pertahun += $_POST[$i];
			}

			$total =  $_POST['gaji_honor']+$_POST['barang_tak_habis']+$_POST['barang_habis']+$_POST['perjalanan']+$_POST['jasa_honor_ext'];
			
			$perbulan = $pertahun!=0?$total/$pertahun:0;
			
			$temp_bulan = array();
			
			//array for month
			for($i=1;$i<=12;$i++){
				$temp_bulan[$this->__int_to_month($i)]= $_POST[$i]!=false?$perbulan:0;
			}

			//array_merge(arrays)
			$data = array (
				'key'=>$_POST['id_program'].'.'.$_POST['id_activity'],
				'name_activity'=>$_POST['name_activity'],
				'desc_activity'=>$_POST['name_activity'],
				'total'=>$total,
				'gaji_honor'=>$_POST['gaji_honor'],
				'barang_tak_habis'=>$_POST['barang_tak_habis'],
				'barang_habis'=>$_POST['barang_habis'],
				'perjalanan'=>$_POST['perjalanan'],
				'jasa_honor_ext'=>$_POST['jasa_honor_ext'],
				'volume'=>$_POST['volume'],
				'operational'=>$_POST['operational'],
				'key_bud_m_yayasan_activity'=>$_POST['id_yayasan'],
				'key_bud_r_sumber'=>$_POST['id_source'],
				'key_bud_r_unit'=>$_POST['id_unit'],
				'key_bud_m_version'=>$this->versi,
				'key_bud_m_program'=>$_POST['id_program'],
			);
			
			$data = array_merge($data,$temp_bulan);//merging
			$this->common->who_am_i($data, true, 'crea');
			$this->db->where('id', $_POST['id']);
			$this->db->update('bud_m_d_activity', $data); 
			$this->common->flash_message('Succesfully added',-1);	
			redirect("bud/bud_activity/view");
		}

		function delete($id)
		{
			$this->load->model($this->model_path)->delete(array('id'=>$id));
			$this->common->flash_message('Record '.$id.' deleted', -1);
			$this->db->query("DELETE FROM bud_m_version WHERE `key` NOT IN (SELECT DISTINCT(`key_bud_m_version`) FROM bud_m_d_activity)");
			redirect('bud/bud_activity');
		}
	}
?>